package com.idp.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.idp.common.base.BaseController;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import com.idp.system.dto.SysRoleDto;
import com.idp.system.dto.SysRoleMenuDto;
import com.idp.system.dto.SysRoleSearchDto;
import com.idp.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色Controller
 * @author king
 */
@Api(tags="角色")
@RestController
@RequestMapping("/role/")
public class SysRoleController extends BaseController {

	@Autowired
	private SysRoleService sysRoleService;

	/**
	 * 分页查询
	 * @param sysRoleSearchDto
	 * @return
	 */
	@ApiOperation("分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"),
			@ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")
	})
	@PostMapping("findByPage")
	public ResultModel<PageModel<SysRoleDto>> findByPage(@RequestBody SysRoleSearchDto sysRoleSearchDto) {
		return ResultModel.success(sysRoleService.findByPage(sysRoleSearchDto));
	}

	/**
	 * 通过id查询
	 * @param sysRoleDto
	 * @return
	 */
	@ApiOperation("通过id查询")
	@ApiOperationSupport(includeParameters = {"sysRoleDto.id"})
	@PostMapping("getById")
	public ResultModel<SysRoleDto> getById(@RequestBody SysRoleDto sysRoleDto) {
		return ResultModel.success(sysRoleService.getById(sysRoleDto.getId()));
	}

	/**
	 * 新增
	 * @param sysRoleDto
	 * @return
	 */
	@ApiOperation("新增")
	@ApiOperationSupport(ignoreParameters = {"sysRoleDto.id"})
	@PostMapping("add")
	public ResultModel add(@RequestBody SysRoleDto sysRoleDto) {
		sysRoleService.add(sysRoleDto);
		return ResultModel.success("新增成功", null);
	}

	/**
	 * 修改
	 * @param sysRoleDto
	 * @return
	 */
	@ApiOperation("修改")
	@PostMapping("update")
	public ResultModel update(@RequestBody SysRoleDto sysRoleDto) {
		sysRoleService.update(sysRoleDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 删除
	 * @param sysRoleDto
	 * @return
	 */
	@ApiOperation("删除")
	@ApiOperationSupport(includeParameters = {"sysRoleDto.id"})
	@PostMapping("delete")
	public ResultModel delete(@RequestBody SysRoleDto sysRoleDto) {
		sysRoleService.delete(sysRoleDto.getId());
		return ResultModel.success("删除成功", null);
	}

	/**
	 * 通过角色id查询菜单按钮权限
	 * @param sysRoleMenuDto
	 * @return
	 */
	@ApiOperation("通过角色id查询菜单按钮权限")
	@ApiOperationSupport(includeParameters = {"sysRoleMenuDto.roleId"})
	@PostMapping("findMenusByRoleId")
	public ResultModel findMenusByRoleId(@RequestBody SysRoleMenuDto sysRoleMenuDto) {
		return ResultModel.success(sysRoleService.findMenusByRoleId(sysRoleMenuDto.getRoleId()));
	}

	/**
	 * 查询所有角色
	 * @return
	 */
	@ApiOperation("查询所有角色")
	@PostMapping("findAll")
	public ResultModel<List<SysRoleDto>> findAll() {
		return ResultModel.success(sysRoleService.findAll());
	}

}
